var path = require('path'),
    webpack = require('webpack'),
    ExtractTextPlugin = require('extract-text-webpack-plugin'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    TransferWebpackPlugin = require('transfer-webpack-plugin'),
    cssnano = require('cssnano'),
    autoprefixer = require('autoprefixer');

var ROOT_PATH = path.resolve(__dirname);

var config = {
  entry: {
    vendor: path.resolve(ROOT_PATH,'src/vendor'),
    main: path.resolve(ROOT_PATH,'src/main')
  },
  output: {
    path: path.resolve(ROOT_PATH,'build'),
    publicPath: '../',
    filename: 'js/[name].js'
  },
  devServer: {
    historyApiFallback: true,
    hot: true,
    inline: true,
    progress: true,
  },
  module: {
    noParse: [/\.min\.js$/,/vue-animated-list/],
    loaders: [{
      test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
      loader: 'url?limit=9000&name=imgs/[name].[ext]'
    },{
      test: /\.css$/,
      loader: ExtractTextPlugin.extract('style-loader','css-loader')
    },{
      test: /\.scss$/,
      loader: ExtractTextPlugin.extract('style-loader','css-loader!postcss-loader!sass-loader')
    },{
      test: /\.jsx?$/,
      loader: 'babel',
      exclude: /node_modules/,
      query: { presets: ['es2015'] }
    }]
  },
  postcss: function () {
    return [autoprefixer,cssnano];
  },
  plugins: [
    new ExtractTextPlugin('css/style.css'),
    new webpack.optimize.UglifyJsPlugin({
      exclude: /main\.js($|\?)/i,
      compress: { warnings: false },
      output: { comments: false }
    }),
    new TransferWebpackPlugin([
          { from: 'imgs', to : 'imgs'},
          { from: 'io', to : 'js'}
        ], path.join(__dirname, 'src')
      )
  ]
};

// 读取输出模板
var pages = [ 
  'question_detail.html', 
  'question_list.html' 
];

pages.forEach(function (pathname) {
  var conf = {
    filename: pathname,
    template: 'src/template/' + pathname,
    inject: false,
  };
  config.plugins.push(new HtmlWebpackPlugin(conf));
});


module.exports = config;